package com.ttflutter.tkvideodownloader

import android.util.Log
import com.appsflyer.AFInAppEventParameterName
import com.appsflyer.AppsFlyerConversionListener
import com.appsflyer.AppsFlyerLib

/**
 * @author Afra55
 * @date 2020/8/2
 * A smile is the best business card.
 * tikvideossuper@163.com / TIKvideo123!
 */
object AppsflyerUtils {
    val LOG_TAG = "AppsflyerUtils"
    var isInited = false
    fun init() {
        try {
            val conversionDataListener = object : AppsFlyerConversionListener {
                override fun onConversionDataSuccess(data: MutableMap<String, Any>?) {
                    if (ZHelp.isDebugMode) {
                        data?.let { cvData ->
                            cvData.map {
                                Log.i(LOG_TAG, "conversion_attribute:  ${it.key} = ${it.value}")
                            }
                        }
                    }
                }

                override fun onConversionDataFail(error: String?) {
                    if (ZHelp.isDebugMode)
                        Log.e(LOG_TAG, "error onAttributionFailure :  $error")
                }

                override fun onAppOpenAttribution(data: MutableMap<String, String>?) {
                    if (ZHelp.isDebugMode)
                        data?.map {
                            Log.d(LOG_TAG, "onAppOpen_attribute: ${it.key} = ${it.value}")
                        }
                }

                override fun onAttributionFailure(error: String?) {
                    if (ZHelp.isDebugMode)
                        Log.e(LOG_TAG, "error onAttributionFailure :  $error")
                }
            }

            AppsFlyerLib.getInstance().init("KvHvQgfRns9feGBitvFkPV", conversionDataListener, AppApplication.context)
            if (ZHelp.isDebugMode)
                AppsFlyerLib.getInstance().setDebugLog(ZHelp.isDebugMode)
            AppsFlyerLib.getInstance().startTracking(AppApplication.context)
            if (ZHelp.isDebugMode) {
                Log.i("AppsFlyer", "init success")
            }
            isInited = true
        } catch (e: Exception) {
            e.printStackTrace()
            if (ZHelp.isDebugMode) {
                Log.i("AppsFlyer", "init error", e)
            }
        }
    }


    fun trackEvent(eventName: String, map: HashMap<String, Any>?) {
        if (!isInited) {
            init()
        }
        try {
            AppsFlyerLib.getInstance().trackEvent(AppApplication.context, eventName, map)
            if (ZHelp.isDebugMode) {
                Log.i("AppsFlyer", "trackEvent $eventName")
            }
        } catch (e: Exception) {
            e.printStackTrace()
            if (ZHelp.isDebugMode) {
                Log.i("AppsFlyer", "trackEvent $eventName", e)
            }
        }
    }


    fun track_purchase_success(
        REVENUE: String?,
        CONTENT_TYPE: String?,
        CONTENT_ID: String?,
        CURRENCY: String = "USD"
    ) {
        val eventValue = HashMap<String, Any>()
        if (REVENUE != null) {
            eventValue.put(AFInAppEventParameterName.REVENUE, REVENUE)
        }
        if (CONTENT_TYPE != null) {
            eventValue.put(AFInAppEventParameterName.CONTENT_TYPE, CONTENT_TYPE)
        }
        if (CONTENT_ID != null) {
            eventValue.put(AFInAppEventParameterName.CONTENT_ID, CONTENT_ID)
        }
        eventValue.put(AFInAppEventParameterName.CURRENCY, CURRENCY)
        trackEvent("purchase_success", null)
    }

    fun adRequest() {
        trackEvent("ad_request", null)
    }

    fun adShouldShow() {
        trackEvent("ad_should_impression", null)
    }
    fun adRealShow() {
        trackEvent("ad_impression", null)
    }
}